package foodmaterial.action;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import foodmaterial.action.valid.ObjectNullValid;
import foodmaterial.config.ChoiceDataSource;
import foodmaterial.config.DataSourceContextHolder;
import foodmaterial.dto.*;
import foodmaterial.entity.*;
import foodmaterial.excel.Excel;
import foodmaterial.service.FoodMaterialService;
import foodmaterial.service.InitService;
import foodmaterial.utils.Constants;
import foodmaterial.utils.FileTools;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.Size;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RequestMapping("/api/foodMaterial")
@RestController
@CrossOrigin(allowCredentials = "true")
public class FoodMaterialAction extends BaseAction {
    @Autowired
    private FoodMaterialService service ;

    @Autowired
    private InitService initService ;

    @Autowired
    private Excel excel;

    public static class Supply {
        @JsonFormat(pattern=Constants.DEFAULT_DATE_FORMAT,timezone="GMT+8")
        @ObjectNullValid(message = "食材供应开始日期不能为空")
        Date beginDate ;
        @JsonFormat(pattern=Constants.DEFAULT_DATE_FORMAT,timezone="GMT+8")
        @ObjectNullValid(message = "食材供应截至日期不能为空")
        Date endDate ;
        private Boolean originalProcess ;
        @Size(min= 1,message = "食材供应最少有一条明细")
        List<Long> foodMaterialId ;

        public Date getBeginDate() {
            return beginDate;
        }

        public void setBeginDate(Date beginDate) {
            this.beginDate = beginDate;
        }

        public Date getEndDate() {
            return endDate;
        }

        public void setEndDate(Date endDate) {
            this.endDate = endDate;
        }

        public Boolean getOriginalProcess() {
            return originalProcess;
        }

        public void setOriginalProcess(Boolean originalProcess) {
            this.originalProcess = originalProcess;
        }

        public List<Long> getFoodMaterialId() {
            return foodMaterialId;
        }

        public void setFoodMaterialId(List<Long> foodMaterialId) {
            this.foodMaterialId = foodMaterialId;
        }
    }

    @PostMapping("/addFoodMaterialType")
    @ChoiceDataSource
    public JsonResult<String> addFoodMaterialType(@RequestBody @Valid FoodMaterialType foodMaterialType,
                                                  BindingResult bindingResult) {
        foodMaterialType.setDb(getSysUser().getDb());
        JsonResult jsonResult = BindingResultUtil.error(bindingResult) ;
        if (jsonResult.getSuccess() == false) return jsonResult ;
        String result = service.addFoodMaterialType(foodMaterialType) ;
        return StringUtils.isEmpty(result) ? JsonResultTool.get(jsonResult,true, "新增食材分类成功") :
                JsonResultTool.get(jsonResult,false,result) ;
    }

    @PostMapping("/modifyFoodMaterialType")
    @ChoiceDataSource
    public JsonResult<String> modifyFoodMaterialType(@RequestBody @Valid FoodMaterialType foodMaterialType,
                                                     BindingResult bindingResult) {
        foodMaterialType.setDb(getSysUser().getDb());
        JsonResult jsonResult = BindingResultUtil.error(bindingResult) ;
        if (jsonResult.getSuccess() == false) return jsonResult ;
        String result = service.modifyFoodMaterialType(foodMaterialType) ;
        return StringUtils.isEmpty(result) ? JsonResultTool.get(jsonResult,true, "修改食材分类成功") :
                JsonResultTool.get(jsonResult,false,result) ;
    }

    @PostMapping("/queryFoodMaterialType")
    @ChoiceDataSource
    public JsonResult<PageInfo<FoodMaterialType>> queryFoodMaterialType(
            @RequestBody @Valid FoodMaterialTypeDto dto,BindingResult bindingResult) {
        JsonResult jsonResult = BindingResultUtil.error(bindingResult) ;
        if (jsonResult.getSuccess() == false) return jsonResult ;
        List<FoodMaterialType> result = null ;
        if (dto.getPageNo() != null) {
            this.setPageHelper(dto.getPageNo(),dto.getPageSize());
            result = service.queryFoodMaterialType(dto);
        } else {
            result = service.queryFoodMaterialType(getSysUser().getDb());
        }
        PageInfo<FoodMaterialType> pageInfo = new PageInfo<>(result) ;
        jsonResult.setData(pageInfo);
        jsonResult.setErrorMsg(ResultCode.SUCCESS.getMessage());
        return jsonResult ;
        //return new JsonResult(true, ResultCode.SUCCESS,pageInfo) ;
    }

    @PostMapping("/addFoodMaterialUnit")
    @ChoiceDataSource
    public JsonResult<String> addFoodMaterialUnit(@RequestBody @Valid FoodMaterialUnit foodMaterialUnit,
                                                  BindingResult bindingResult) {
        JsonResult jsonResult = BindingResultUtil.error(bindingResult) ;
        if (jsonResult.getSuccess() == false) return jsonResult ;
        String result = service.addFoodMaterialUnit(foodMaterialUnit) ;
        return StringUtils.isEmpty(result)  ? JsonResultTool.get(jsonResult,true, "新增食材计量单位成功") :
                JsonResultTool.get(jsonResult,false,result) ;
    }

    @PostMapping("/modifyFoodMaterialUnit")
    @ChoiceDataSource
    public JsonResult<String> modifyFoodMaterialUnit(@RequestBody @Valid FoodMaterialUnit foodMaterialUnit,
                                                     BindingResult bindingResult) {
        JsonResult jsonResult = BindingResultUtil.error(bindingResult) ;
        if (jsonResult.getSuccess() == false) return jsonResult ;
        String result = service.modifyFoodMaterialUnit(foodMaterialUnit) ;
        return StringUtils.isEmpty(result) ? JsonResultTool.get(jsonResult,true, "修改食材计量单位成功") :
                JsonResultTool.get(jsonResult,false,result) ;
    }

    @PostMapping("/queryFoodMaterialUnit")
    @ChoiceDataSource
    public JsonResult<PageInfo<FoodMaterialUnit>> queryFoodMaterialType(@RequestBody FoodMaterialUnit foodMaterialUnit) {
        PageHelper.startPage(foodMaterialUnit.getPageNo(),foodMaterialUnit.getPageSize()) ;
        List<FoodMaterialUnit> result = service.queryFoodMaterialUnit(foodMaterialUnit) ;
        PageInfo<FoodMaterialUnit> pageInfo = new PageInfo<>(result) ;
        return new JsonResult(true, ResultCode.SUCCESS,pageInfo) ;
    }

    @PostMapping("/addFoodMaterial")
    @ChoiceDataSource
    public JsonResult<String> addFoodMaterial(@RequestBody @Valid FoodMaterial foodMaterial,BindingResult bindingResult) {
        JsonResult jsonResult = BindingResultUtil.error(bindingResult) ;
        if (jsonResult.getSuccess() == false) return jsonResult ;
        String result = service.addFoodMaterial(foodMaterial) ;
        return StringUtils.isEmpty(result) ? JsonResultTool.get(jsonResult,true, "新增食材成功") :
                JsonResultTool.get(jsonResult,false,result) ;

    }

    @PostMapping("/modifyFoodMaterial")
    @ChoiceDataSource
    public JsonResult<String> modifyFoodMaterial(@RequestBody @Valid FoodMaterial foodMaterial,BindingResult bindingResult) {
        JsonResult jsonResult = BindingResultUtil.error(bindingResult) ;
        if (jsonResult.getSuccess() == false) return jsonResult ;
        String result = service.modifyFoodMaterial(foodMaterial) ;
        return StringUtils.isEmpty(result) ? JsonResultTool.get(jsonResult,true, "修改食材成功") :
                JsonResultTool.get(jsonResult,false,result) ;
    }

    @PostMapping("/stopFoodMaterial")
    @ChoiceDataSource
    public JsonResult<String> stopFoodMaterial(@RequestBody Map<String,Object> params){
        int result = service.stopFoodMaterial(params) ;
        return result > 0 ? new JsonResult(true, "修改成功") :
                new JsonResult(false,"修改失败") ;
    }

    @PostMapping("/batchModifyFoodMaterialPrice")
    @ChoiceDataSource
    public JsonResult<String> batchModifyFoodMaterialPrice(@RequestBody Map<String,List<FoodMaterial>> foodMaterials) {
        JsonResult jsonResult = new JsonResult() ;
        String result = service.batchModifyFoodMaterialPrice(foodMaterials.get("foodMaterials")) ;
        return result == null ? JsonResultTool.get(jsonResult,true, "批量修改成功") :
                JsonResultTool.get(jsonResult,false,result) ;
    }

    @PostMapping("/queryFoodMaterial")
    @ChoiceDataSource
    public JsonResult<PageInfo<FoodMaterial>> queryFoodMaterial(@RequestBody @Valid FoodMaterialDto dto,BindingResult bindingResult) {
        JsonResult jsonResult = BindingResultUtil.error(bindingResult) ;
        if (jsonResult.getSuccess() == false) return jsonResult ;
        setPageHelper(dto.getPageNo(),dto.getPageSize()) ;
        PageInfo<FoodMaterial> pageInfo = new PageInfo<>(service.queryFoodMaterial(dto)) ;
        jsonResult.setData(pageInfo);
        jsonResult.setErrorMsg(ResultCode.SUCCESS.getMessage());
        return jsonResult ;
        //return new JsonResult(true, ResultCode.SUCCESS,pageInfo) ;
    }

    @PostMapping("/querySupplyFoodMaterial")
    @ChoiceDataSource
    public JsonResult<PageInfo<FoodMaterial>> querySupplyFoodMaterial(@RequestBody FoodMaterialDto dto) {
        PageHelper.startPage(dto.getPageNo(),dto.getPageSize()) ;
        List<FoodMaterial> result = service.querySupplyFoodMaterial(dto) ;
        PageInfo<FoodMaterial> pageInfo = new PageInfo<>(result) ;
        return new JsonResult(true, ResultCode.SUCCESS,pageInfo) ;
    }

    @PostMapping("/queryVipFoodMaterial")
    @ChoiceDataSource
    public JsonResult<PageInfo<FoodMaterial>> queryVipFoodMaterial(@RequestBody FoodMaterialDto dto) {
        PageHelper.startPage(dto.getPageNo(),dto.getPageSize()) ;
        List<FoodMaterial> result = service.queryVipFoodMaterial(dto) ;
        PageInfo<FoodMaterial> pageInfo = new PageInfo<>(result) ;
        return new JsonResult(true, ResultCode.SUCCESS,pageInfo) ;
    }

    @PostMapping("/queryVipSupplyFoodMaterial")
    @ChoiceDataSource
    public JsonResult<PageInfo<FoodMaterial>> queryVipSupplyFoodMaterial(@RequestBody FoodMaterialDto dto) {
        PageHelper.startPage(dto.getPageNo(),dto.getPageSize()) ;
        List<FoodMaterial> result = service.queryVipSupplyFoodMaterial(dto) ;
        PageInfo<FoodMaterial> pageInfo = new PageInfo<>(result) ;
        return new JsonResult(true, ResultCode.SUCCESS,pageInfo) ;
    }

    @PostMapping("/queryFoodMaterialById")
    @ChoiceDataSource
    public JsonResult<FoodMaterial> queryFoodMaterialById(@RequestBody FoodMaterialDto dto) {
        List<FoodMaterial> result = service.queryFoodMaterial(dto) ;
        FoodMaterial foodMaterial1 = result.isEmpty()? null : result.get(0) ;
        return (foodMaterial1 != null) ? new JsonResult(true,ResultCode.SUCCESS,foodMaterial1) :
                new JsonResult(false,"食材不存在");
    }

    @PostMapping("/addFoodMaterialSupply")
    @ChoiceDataSource
    public JsonResult<String> addFoodMaterialSupply(@RequestBody @Valid Supply supply,BindingResult bindingResult) {
        JsonResult jsonResult = BindingResultUtil.error(bindingResult) ;
        if (jsonResult.getSuccess() == false) return jsonResult ;
        String result = service.addFoodMaterialSupply(supply) ;
        return result == null ? JsonResultTool.get(jsonResult,true, "新增供应信息成功") :
                JsonResultTool.get(jsonResult,false,result) ;
    }

    @PostMapping("/removeFoodMaterialSupply")
    @ChoiceDataSource
    public JsonResult<String> removeFoodMaterialSupply(@RequestBody FoodMaterialSupplyDto dto) {
        String result = service.removeFoodMaterialSupply(dto) ;
        return StringUtils.isEmpty(result) ? new JsonResult(true, "删除成功") :
                new JsonResult(false,result)  ;
    }

    @PostMapping("/queryFoodMaterialSupply")
    @ChoiceDataSource
    public JsonResult<PageInfo<FoodMaterialSupply>> queryFoodMaterialSupply(
            @RequestBody FoodMaterialSupplyDto dto) {
        int pageNo = 1 ;
        int pageSize = 10 ;
        if (dto.getPageNo() != null) pageNo = dto.getPageNo();
        if (dto.getPageSize() != null) pageSize = dto.getPageSize() ;
        PageHelper.startPage(pageNo,pageSize) ;
        List<FoodMaterialSupply> result = service.queryFoodMaterialSupply(dto) ;
        PageInfo<FoodMaterialSupply> pageInfo = new PageInfo<>(result) ;
        return new JsonResult(true, ResultCode.SUCCESS,pageInfo) ;
    }

    public static class CustomerPriceChoice {
        private Long templateId ;
        private List<Long> customerIds ;
        private Boolean originalProcess ;

        public Long getTemplateId() {
            return templateId;
        }

        public void setTemplateId(Long templateId) {
            this.templateId = templateId;
        }

        public List<Long> getCustomerIds() {
            return customerIds;
        }

        public void setCustomerIds(List<Long> customerIds) {
            this.customerIds = customerIds;
        }

        public Boolean getOriginalProcess() {
            return originalProcess;
        }

        public void setOriginalProcess(Boolean originalProcess) {
            this.originalProcess = originalProcess;
        }
    }

    @PostMapping("/addCustomerPrice")
    @ChoiceDataSource
    public JsonResult<String> addCustomerPrice(@RequestBody @Valid CustomerPriceChoice customerPrice,
                                               BindingResult bindingResult) {
        JsonResult jsonResult = BindingResultUtil.error(bindingResult) ;
        if (jsonResult.getSuccess() == false) return jsonResult ;
        int result = service.addCustomerPrice(customerPrice) ;
        return result > 0 ? JsonResultTool.get(jsonResult,true, "维护价格成功") :
                JsonResultTool.get(jsonResult,false,"维护价格失败") ;
    }

    @PostMapping("/removeCustomerPrice")
    @ChoiceDataSource
    public JsonResult<String> removeCustomerPrice(@RequestBody CustomerPriceDto dto) {
        int result = service.removeCustomerPrice(dto) ;
        return result > 0 ? new JsonResult(true, "删除成功") :
                new JsonResult(false,"删除失败")  ;
    }

    @PostMapping("/queryCustomerPrice")
    @ChoiceDataSource
    public JsonResult<List<CustomerPrice>> queryCustomerPrice(@RequestBody CustomerPriceDto dto) {
        List<CustomerPrice> result = service.queryCustomerPrice(dto) ;
        return new JsonResult(true, ResultCode.SUCCESS,result) ;
    }

    @PostMapping("/queryCustomerPriceDetail")
    @ChoiceDataSource
    public JsonResult<List<CustomerPriceTemplateDetail>> queryCustomerPriceDetail(
          @RequestBody CustomerPriceDto dto) {
        List<CustomerPriceTemplateDetail> result = service.queryCustomerPriceDetail(dto) ;
        return new JsonResult(true, ResultCode.SUCCESS,result) ;
    }

    private JsonResult<String> uploadFoodMaterial(MultipartFile file,boolean originalProcess)throws IOException {
        int pos = file.getOriginalFilename().lastIndexOf('.') ;
        String suffix =file.getOriginalFilename().substring(pos + 1);
        Workbook workbook = Excel.getWorkbook(file.getInputStream(),suffix) ;
        try {
            String result = excel.uploadFoodMaterialExcel(workbook, DataSourceContextHolder.getDBType(),originalProcess);
            return result == null ? new JsonResult(true, "上传成功") :
                    new JsonResult(false,result) ;
        } finally {
            FileTools.close(workbook);
        }
    }

    @PostMapping("/uploadFoodMaterial")
    @ChoiceDataSource
    public JsonResult<String> uploadFoodMaterial(MultipartFile file) throws IOException {
        return uploadFoodMaterial(file,false) ;
    }

    @PostMapping("/uploadProcessFoodMaterial")
    @ChoiceDataSource
    public JsonResult<String> uploadProcessFoodMaterial(MultipartFile file) throws IOException {
        return uploadFoodMaterial(file,true) ;
    }

    private JsonResult<String> uploadFoodMaterialSupply(MultipartFile file,boolean originalProcess)throws IOException {
        int pos = file.getOriginalFilename().lastIndexOf('.') ;
        String suffix =file.getOriginalFilename().substring(pos + 1);
        Workbook workbook = Excel.getWorkbook(file.getInputStream(),suffix) ;
        try {
            String result = excel.uploadFoodMaterialSupplyExcel(workbook, originalProcess,service);
            return result == null ? new JsonResult(true, "上传成功") :
                    new JsonResult(false,result) ;
        } finally {
            FileTools.close(workbook);
        }
    }

    @PostMapping("/uploadFoodMaterialSupply")
    @ChoiceDataSource
    public JsonResult<String> uploadFoodMaterialSupply(MultipartFile file) throws IOException {
        return uploadFoodMaterialSupply(file,false) ;
    }

    @PostMapping("/uploadProcessFoodMaterialSupply")
    @ChoiceDataSource
    public JsonResult<String> uploadProcessFoodMaterialSupply(MultipartFile file) throws IOException {
        return uploadFoodMaterialSupply(file,true) ;
    }

    @GetMapping("/downloadFoodMaterial")
    @ChoiceDataSource
    public void downloadFoodMaterial(@RequestParam(value = "originalProcess") boolean originalProcess,
                                     HttpServletResponse response) throws IOException {
        this.setDownload(response,excel);
        if (originalProcess == false)
            excel.downloadFoodMaterialExcel(
                    Constants.EXCEL_FOOD_MATERIAL_TEMPLATE,response.getOutputStream(),originalProcess);
        else
            excel.downloadFoodMaterialExcel(
                    Constants.EXCEL_PROCESS_FOOD_MATERIAL_TEMPLATE,response.getOutputStream(),originalProcess);
    }

    @PostMapping("/addCustomerPriceTemplate")
    @ChoiceDataSource
    public JsonResult<String> addCustomerPriceTemplate(
            @RequestBody @Valid CustomerPriceTemplate customerPriceTemplate,
            BindingResult bindingResult) {
        JsonResult jsonResult = BindingResultUtil.error(bindingResult) ;
        if (jsonResult.getSuccess() == false) return jsonResult ;
        int result = service.addCustomerPriceTemplate(customerPriceTemplate) ;
        return result > 0 ? JsonResultTool.get(jsonResult,true, "新增价格模板成功") :
                JsonResultTool.get(jsonResult,false,"新增价格模板失败") ;
    }

    @PostMapping("/modifyCustomerPriceTemplate")
    @ChoiceDataSource
    public JsonResult<String> modifyCustomerPriceTemplate(
            @RequestBody @Valid CustomerPriceTemplate customerPriceTemplate,
            BindingResult bindingResult) {
        JsonResult jsonResult = BindingResultUtil.error(bindingResult) ;
        if (jsonResult.getSuccess() == false) return jsonResult ;
        int result = service.modifyCustomerPriceTemplate(customerPriceTemplate) ;
        return result > 0 ? JsonResultTool.get(jsonResult,true, "修改价格模板成功") :
                JsonResultTool.get(jsonResult,false,"修改价格模板失败") ;
    }

    @GetMapping("/removeCustomerPriceTemplate")
    @ChoiceDataSource
    public JsonResult<String> removeCustomerPriceTemplate(@RequestParam("templateId") Long templateId){
        int result = service.removeCustomerPriceTemplate(templateId) ;
        return result > 0 ? new JsonResult(true, "删除价格模板成功") :
                new JsonResult(false,"删除价格模板失败") ;
    }

    @PostMapping("/queryCustomerPriceTemplate")
    @ChoiceDataSource
    public JsonResult<List<CustomerPriceTemplate>> queryCustomerPriceTemplate(
            @RequestBody CustomerPriceTemplate template) {
        List<CustomerPriceTemplate> result = service.queryCustomerPriceTemplate(template) ;
        return new JsonResult(true, ResultCode.SUCCESS,result) ;
    }

    @PostMapping("/queryCustomerPriceTemplateDetail")
    @ChoiceDataSource
    public JsonResult<List<CustomerPriceTemplateDetail>> queryCustomerPriceTemplateDetail(
            @RequestBody CustomerPriceTemplateDetailDto dto) {
        List<CustomerPriceTemplateDetail> result = service.queryCustomerPriceTemplateDetail(dto) ;
        return new JsonResult(true, ResultCode.SUCCESS,result) ;
    }

    @GetMapping("/clearFoodMaterial")
    @ChoiceDataSource
    public JsonResult<String> clearFoodMaterial() {
        String result = service.clearFoodMaterial();
        return result == null ? new JsonResult(true,"清理成功"):
                new JsonResult(false,result);
    }

    @GetMapping("/downloadFoodMaterialTemplate")
    @ChoiceDataSource
    public void downloadFoodMaterialTemplate(
            HttpServletResponse response) throws IOException {
        setDownload(response,"foodMaterialTemplate.xlsx");
        StringBuilder imageSavePath = new StringBuilder();
        imageSavePath.append(initService.querySysParamsByCode(Constants.EXCEL_TEMPLATE_PATH).getParamValue()) ;
        imageSavePath.append("foodMaterialTemplate.xlsx") ;
        ReadImage.readImage(imageSavePath.toString(),response);
    }

    @GetMapping("/downloadProcessFoodMaterialTemplate")
    @ChoiceDataSource
    public void downloadProcessFoodMaterialTemplate(
            HttpServletResponse response) throws IOException {
        setDownload(response,"processFoodMaterialTemplate.xlsx");
        StringBuilder imageSavePath = new StringBuilder();
        imageSavePath.append(initService.querySysParamsByCode(Constants.EXCEL_TEMPLATE_PATH).getParamValue()) ;
        imageSavePath.append("processFoodMaterialTemplate.xlsx") ;
        ReadImage.readImage(imageSavePath.toString(),response);
    }

    @GetMapping("/downloadFoodMaterialSupplyTemplate")
    @ChoiceDataSource
    public void downloadFoodMaterialSupplyTemplate(
            HttpServletResponse response) throws IOException {
        setDownload(response,"foodMaterialTemplateSupply.xlsx");
        StringBuilder imageSavePath = new StringBuilder();
        imageSavePath.append(initService.querySysParamsByCode(Constants.EXCEL_TEMPLATE_PATH).getParamValue()) ;
        imageSavePath.append("foodMaterialTemplateSupply.xlsx") ;
        ReadImage.readImage(imageSavePath.toString(),response);
    }

    @PostMapping("/addTestReport")
    @ChoiceDataSource
    public JsonResult<String> addTestReport(@RequestBody @Valid TestReport testReport,
                                            BindingResult bindingResult) {
        JsonResult jsonResult = BindingResultUtil.error(bindingResult) ;
        if (jsonResult.getSuccess() == false) return jsonResult ;
        int result = service.addTestReport(testReport) ;
        return result > 0 ? JsonResultTool.get(jsonResult,true, "新增检测报告成功") :
                JsonResultTool.get(jsonResult,false,"新增检测报告失败") ;
    }

    @PostMapping("/modifyTestReport")
    @ChoiceDataSource
    public JsonResult<String> modifyTestReport(@RequestBody @Valid TestReport testReport,
                                               BindingResult bindingResult) {
        JsonResult jsonResult = BindingResultUtil.error(bindingResult) ;
        if (jsonResult.getSuccess() == false) return jsonResult ;
        int result = service.modifyTestReport(testReport) ;
        return result > 0 ? JsonResultTool.get(jsonResult,true, "修改检测报告成功") :
                JsonResultTool.get(jsonResult,false,"修改检测报告失败") ;
    }

    @GetMapping("/removeTestReport")
    @ChoiceDataSource
    public JsonResult<String> removeTestReport(@RequestParam("reportId")  Long id) {
        int result = service.removeTestReport(id) ;
        return result > 0 ? new JsonResult(true,"删除检测报告成功"):
                new JsonResult(false,"你删除的检测报告不存在") ;
    }

    @PostMapping("/queryTestReport")
    @ChoiceDataSource
    public JsonResult<PageInfo<TestReport>> queryTestReport(@RequestBody TestReportDto dto) {
        PageHelper.startPage(dto.getPageNo(),dto.getPageSize()) ;
        List<TestReport> result = service.queryTestReport(dto) ;
        PageInfo<TestReport> pageInfo = new PageInfo<>(result) ;
        return new JsonResult(true, ResultCode.SUCCESS,pageInfo) ;
    }

    @GetMapping("/queryTestReportById")
    @ChoiceDataSource
    public JsonResult<TestReport> queryTestReportById(@RequestParam("reportId") Long id) {
        TestReport result = service.queryTestReportById(id) ;
        return (result != null) ? new JsonResult(true,ResultCode.SUCCESS,result) :
                new JsonResult(false,"检测报告不存在");
    }

    @GetMapping("/downloadTestReport")
    @ChoiceDataSource
    public void downloadTestReport(
            @RequestParam("fileName") String fileName,
            HttpServletResponse response) throws IOException {
        setDownload(response,fileName);
        StringBuilder imageSavePath = new StringBuilder();
        SysUser userDetails = (SysUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        imageSavePath.append(initService.querySysParamsByCode(Constants.TEST_REPORT_PATH).getParamValue()) ;
        imageSavePath.append(userDetails.getDb()).append('/').append(fileName) ;
        ReadImage.readImage(imageSavePath.toString(),response);
    }
}
